package Java面试周阳.线程不安全的集合;

import java.util.*;
import java.util.concurrent.CopyOnWriteArraySet;

public class HashSetNotSafeDemo {
    public static void main(String[] args) {
//        demo01();
//        demo02();
        demo03();
    }
    private static void demo03() {
        Set<String> set = new CopyOnWriteArraySet<>();
        for(int i=0; i<30; i++) {
            new Thread(() -> {
                set.add(UUID.randomUUID().toString().substring(0, 8));
                System.out.println(set);
            }, String.valueOf(i)).start();
        }
    }

    private static void demo02() {
        Set<String> set = new HashSet<>();
        for(int i=0; i<30; i++) {
            new Thread(() -> {
                set.add(UUID.randomUUID().toString().substring(0, 8));
                System.out.println(set);
            }, String.valueOf(i)).start();
        }
    }

    private static void demo01() {
        Set<String> set = new HashSet<>();
        set.add("aaa");
        set.add("bbb");
        set.add("ccc");
        for (String element : set){
            System.out.println(element);
        }
    }
}
